package cn.kgc.crowdfundingadminspringboot2.service;

import cn.kgc.crowdfundingadminspringboot2.mapper.RoleMapper;
import cn.kgc.crowdfundingadminspringboot2.pojo.Role;
import cn.kgc.crowdfundingadminspringboot2.pojo.RoleExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    RoleMapper roleMapper;

    @Override
    public PageInfo<Role> queryForKeywordWithPage(Integer pageNum, Integer pageSize, String keyword) {
        // 1.开启分页功能
        PageHelper.startPage(pageNum, pageSize);

        // 2.执行查询
        RoleExample roleExample=new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andNameLike("%"+keyword+"%");
        List<Role> list = roleMapper.selectByExample(roleExample);
        // 3.封装为PageInfo对象
        return new PageInfo<>(list);
    }

    @Override
    public List<Role> getRoleListByIdList(List<Integer> roleIdList) {
        // 创建实体类Role对应的Example对象
        RoleExample roleExample = new RoleExample();

        // 在Example对象中封装查询条件
        roleExample.createCriteria().andIdIn(roleIdList);

        // 执行查询
        return roleMapper.selectByExample(roleExample);
    }


    @Override
    public void batchRemove(List<Integer> roleIdList) {

        RoleExample roleExample = new RoleExample();

        roleExample.createCriteria().andIdIn(roleIdList);

        roleMapper.deleteByExample(roleExample);
    }
}
